package com.itheima.hmshopping.app.controller;

import com.itheima.hmshopping.app.service.SmsCouponService;
import com.itheima.hmshopping.dto.SmsCouponDto;
import com.itheima.hmshopping.util.HmPage;
import com.itheima.hmshopping.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("coupon")
public class SmsCouponController {

    @Autowired
    public SmsCouponService smsCouponService;

    //根据优惠券名称和类型分页获取优惠券列表
    @GetMapping("list")
    public R list(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize,
                  String name,String type){
        HmPage hmPage=smsCouponService.list(name,pageNum,pageSize,type);
        return R.succeed(hmPage,"ok");
    }


    //添加优惠券
    @PostMapping("create")
    public R create(@RequestBody SmsCouponDto smsCouponDto){
        smsCouponService.insert(smsCouponDto);
        return R.succeed("ok","");
    }

    //删除优惠卷
    @PostMapping("delete/{id}")
    public R delect(@PathVariable Integer id){
        smsCouponService.delete(id);
        return R.succeed("ok","");
    }

    //获取单个优惠券的详细信息
    @GetMapping("/{id}")
    public R selectById(@PathVariable Integer id){
        SmsCouponDto smsCouponDto=smsCouponService.selectById(id);
        return R.succeed(smsCouponDto,"ok");
    }

    //修改优惠券
    @PostMapping("update/{id}")
    public R update(@RequestBody SmsCouponDto smsCouponDto,@PathVariable Integer id){
        smsCouponService.update(smsCouponDto,id);
        return R.succeed("ok","");
    }
}
